from dotenv import load_dotenv
import os
import json


def resolve_env(obj):
    if isinstance(obj, dict):
        return {k: resolve_env(v) for k, v in obj.items()}
    elif isinstance(obj, str) and obj.startswith('${') and obj.endswith('}'):
        var_name = obj[2:-1]
        return os.getenv(var_name, obj)
    return obj


def loadJsonConfig():
    load_dotenv()

    with open('scaffolding.json', 'r') as f:
        scaffoldingConfig = json.load(f)

        return resolve_env(scaffoldingConfig)


def getMysqlConfig():
    return resolved_config['scaffolding']['mysql']


def getRedisConfig():
    return resolved_config['scaffolding']['redis']


def getAiConfig():
    return resolved_config['scaffolding']['ai']


def getJwtConfig():
    return resolved_config['scaffolding']['jwt']


def getRateLimitIpConfig():
    return resolved_config['scaffolding']['security']['ratelimit-ip']


def getRateLimitSvrConfig():
    return resolved_config['scaffolding']['security']['ratelimit-svr']


def getBlacklist4IpConfig():
    return resolved_config['scaffolding']['security']['blacklist4ip']


def getBotUAConfig():
    return resolved_config['scaffolding']['security']['botUA']


def getTrustedReferConfig():
    return resolved_config['scaffolding']['security']['trustedRefer']


def getWhitelist4IpConfig():
    return resolved_config['scaffolding']['security']['whitelist4ip']


def getContentTypeConfig():
    return resolved_config['scaffolding']['security']['contentType']


def getMethodConfig():
    return resolved_config['scaffolding']['security']['method']


def getCorsConfig():
    return resolved_config['scaffolding']['security']['cors']


def getTokenConfig():
    return resolved_config['scaffolding']['token']


def getServerConfig():
    return resolved_config['scaffolding']['server']


def getLocalCacheConfig():
    return resolved_config['scaffolding']['localcache']


def getAlarmConfig():
    return resolved_config['scaffolding']['alarm']


resolved_config = loadJsonConfig()
